package com.hand.api.controller.v1;

import com.hand.api.controller.v1.dto.SoHeaderDTO;
import com.hand.api.controller.v1.dto.SoHeaderRequestParam;
import com.hand.app.service.SoHeaderService;
import com.hand.config.SwaggerApiConfig;
import com.hand.domain.entity.SoHeader;
import com.hand.domain.entityvo.SoOrderVo;
import com.hand.domain.repository.SoHeaderRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * 业务表 管理 API
 */
@Api(tags = SwaggerApiConfig.SO_HEADER)
@RestController("soHeaderController.v1")
@RequestMapping("/v1/{organizationId}/so-headers")
public class SoHeaderController extends BaseController {

    @Resource
    private SoHeaderRepository soHeaderRepository;
    @Resource
    private SoHeaderService soHeaderService;

    /**
     * 订单汇总查询API
     * @param requestParam
     * @param pageRequest
     * @param organizationId
     * @return
     */

    @ApiOperation(value = "订单汇总查询API")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<SoHeaderDTO>> queryOrderForAll(
            SoHeaderRequestParam requestParam,
            @ApiIgnore @SortDefault(value = SoHeader.FIELD_ORDER_NUMBER, direction = Sort.Direction.DESC) PageRequest pageRequest,
            @PathVariable String organizationId) {
        Page<SoHeaderDTO> list = soHeaderService.queryOrderForAll(pageRequest, requestParam);
        return Results.success(list);
    }

    /**
     * 订单头查询API
     * @param soHeaderId
     * @param organizationId
     * @return
     */

    @ApiOperation(value = "订单头查询API")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{soHeaderId}")
    public ResponseEntity<SoHeader> queryOrderHeader(@PathVariable Long soHeaderId, @PathVariable String organizationId) {
        SoHeader soHeader = soHeaderRepository.selectByPrimaryKey(soHeaderId);
        return Results.success(soHeader);
    }

    /**
     * 订单删除API
     * @param soHeader
     * @param organizationId
     * @return
     */
    @ApiOperation(value = "订单删除API")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> deleteOrderHeader(@RequestBody SoHeader soHeader, @PathVariable String organizationId) {
        soHeaderService.deleteOrderHeader(soHeader);
        return Results.success();
    }

    /**
     * 订单保存API
     * @param soOrderVo
     * @param organizationId
     * @return
     */

    @ApiOperation(value = "订单保存API")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<SoOrderVo> saveOrder(@RequestBody SoOrderVo soOrderVo, @PathVariable String organizationId) {
        soHeaderService.saveOrder(soOrderVo);
        return Results.success(soOrderVo);
    }
}
